<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一个新的数据库引用</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongodbref.html">MongoDBRef</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodbref.get.html">MongoDBRef::get</a></div>
 <div class="up"><a href="class.mongodbref.html">MongoDBRef</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongodbref.create" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDBRef::create</h1>
  <p class="verinfo">(PECL mongo &gt;= 0.9.0)</p><p class="refpurpose"><span class="refname">MongoDBRef::create</span> &mdash; <span class="dc-title">创建一个新的数据库引用</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodbref.create-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">array</span> <span class="methodname"><strong>MongoDBRef::create</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$collection</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$id</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$database</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   如果没有指定数据库，将会使用当前数据库。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodbref.create-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>

      <em><code class="parameter">collection</code></em>
     </dt>

     <dd>

      <p class="para">
       集合名称（不包括数据库名）。
      </p>
     </dd>

       
    
     <dt>

      <em><code class="parameter">id</code></em>
     </dt>

     <dd>

      <p class="para">
       要链接的对象的 _id 字段。
      </p>
     </dd>

       
    
     <dt>

      <em><code class="parameter">database</code></em>
     </dt>

     <dd>

      <p class="para">
       数据库名。
      </p>
     </dd>

       
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongodbref.create-returnvalues">
  <h3 class="title">返回值</h3>  
  <p class="para">
   返回引用。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodbref.create-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1472">
   <p><strong>Example #1 <span class="function"><strong>MongoDBRef::create()</strong></span> 例子</strong></p>
   <div class="example-contents"><p>
    这个例子为文档创建了一个 <em>addresses</em> 的数据库引用。
    <span class="function"><a href="mongocollection.getname.html" class="function">MongoCollection::getName()</a></span> 函数返回集合的名字（不包括数据库名）。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$addresses&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addresses</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$people&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">people</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;保存&nbsp;$address，所以它有一个&nbsp;_id<br /></span><span style="color: #0000BB">$addresses</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$address</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;创建引用<br /></span><span style="color: #0000BB">$ref&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDBRef</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #0000BB">$addresses</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getName</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">$address</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//设置&nbsp;$person&nbsp;里的字段<br /></span><span style="color: #0000BB">$person</span><span style="color: #007700">[</span><span style="color: #DD0000">'address'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$ref</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">save</span><span style="color: #007700">(</span><span style="color: #0000BB">$person</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongodbref.create-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="mongodb.createdbref.html" class="methodname" rel="rdfs-seeAlso">MongoDB::createDBRef()</a> - Creates a database reference</span></li>
    <li class="member"><span class="methodname"><a href="mongocollection.createdbref.html" class="methodname" rel="rdfs-seeAlso">MongoCollection::createDBRef()</a> - 创建一个数据库引用</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongodbref.html">MongoDBRef</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongodbref.get.html">MongoDBRef::get</a></div>
 <div class="up"><a href="class.mongodbref.html">MongoDBRef</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
